import actionsType from './actionsType'
const initState = {
  list: [],
  menu: []
}
// 7.使用状态管理容器redux，异步请求接口数据并实现列表数据的持久化
const reducer = (state = initState, action) => {
  const { type, payload } = action
  const { list, menu } = state
  switch (type) {
    case actionsType.SET_LIST:
      return {
        ...state,
        list: payload
      };
    case actionsType.SET_MENU:
      return {
        ...state,
        menu: payload
      };
    case actionsType.SWITCH_CODE:
      const item = menu.find(item => item.id === payload.id)
      item.code = !item.code
      return {
        ...state,
        menu: [...menu]
      };
    case actionsType.ADD_MESS:
      return {
        ...state,
        list: list.map(item => {
          if (item.id === payload.id) {
            item.mess.push(payload.info)
          }
          return item
        })
      }
    default:
      return { ...state }
  }
}


export default reducer